/**
 * 
 */
package com.res.simulator.transformer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.res.common.transformer.BaseTransformer;
import com.res.simulator.handler.CommonFileHandler;
import com.res.simulator.view.SearchResultView;
import com.res.simulator.view.RecordAndPlayExecutionResultView;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ScenarioRecord;

/**
 * 
 *
 */
public class RecordedAPIInvocationTransformer implements
    BaseTransformer<List<SearchResultView>, List<RecordAndPlayExecutionResultView>, List<ScenarioRecord>> {

    public List<ScenarioRecord> transformModelToDomian(final List<SearchResultView> searchResultViewList) throws Exception {

        final List<ScenarioRecord> scenarioRecordList = new ArrayList<ScenarioRecord>();
        ApiDefinition apiDefinitionobj = null;
        for (final SearchResultView searchResultView : searchResultViewList) {

            apiDefinitionobj = new ApiDefinition();
            apiDefinitionobj.setApiId(searchResultView.getApiId());
            apiDefinitionobj.setName(searchResultView.getApiName());

            final ScenarioRecord scenarioRecordDomainObj = new ScenarioRecord();
            scenarioRecordDomainObj.setRecordId(searchResultView.getRecordId());
            scenarioRecordDomainObj.setResult(searchResultView.getResult());
            scenarioRecordDomainObj.setConsumeValue(searchResultView.getInputValueString());
            scenarioRecordDomainObj.setApiDefinitionScenarioRecord(apiDefinitionobj);

            scenarioRecordList.add(scenarioRecordDomainObj);

        }

        return scenarioRecordList;
    }

    public List<RecordAndPlayExecutionResultView> transformDomianToView(List<ScenarioRecord> domainList) throws Exception {

        List<RecordAndPlayExecutionResultView> executionResultList = new ArrayList<RecordAndPlayExecutionResultView>();
        RecordAndPlayExecutionResultView executionResultViewobj = null;

        final CommonFileHandler fileHandler = new CommonFileHandler();

        for (final ScenarioRecord scenarioRecord : domainList) {
            executionResultViewobj = new RecordAndPlayExecutionResultView();
            executionResultViewobj.setRecordId(scenarioRecord.getRecordId());
            executionResultViewobj.setApiName(scenarioRecord.getApiDefinitionScenarioRecord().getName());
            executionResultViewobj.setInputValueString(scenarioRecord.getConsumeValue());
            executionResultViewobj.setResult(scenarioRecord.getResult());

            final String fileName = generateFileName(scenarioRecord.getRecordId());
            final String fileUrl = fileHandler.save(scenarioRecord.getResult(), fileName);
            System.out.println("File name=========>" + fileName);
            System.out.println("File url==========>" + fileUrl);

            executionResultViewobj.setLocation(fileUrl);
            executionResultList.add(executionResultViewobj);
        }
        return executionResultList;
    }

    private String generateFileName(Long recordId) throws Exception {

        final Date now = new Date();

        final String channel = "API_" + recordId;

        final String fileName = channel + "-" + now.getTime() + ".xml";

        return fileName;
    }

}
